package com.lyy.lottery.controller;

import com.lyy.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.lyy.lottery.common.errorcode.ServiceErrorCodeConstants;
import com.lyy.lottery.common.exception.ControllerException;
import com.lyy.lottery.common.exception.ServiceException;
import com.lyy.lottery.common.pojo.CommonResult;
import com.lyy.lottery.common.util.JacksonUtil;
import com.lyy.lottery.controller.param.PageParam;
import com.lyy.lottery.controller.result.CreateActivityParam;
import com.lyy.lottery.controller.result.CreateActivityResult;
import com.lyy.lottery.controller.result.FindActivityListResult;
import com.lyy.lottery.controller.result.GetActivityDetailResult;
import com.lyy.lottery.service.ActivityService;
import com.lyy.lottery.service.dto.ActivityDTO;
import com.lyy.lottery.service.dto.ActivityDetailDTO;
import com.lyy.lottery.service.dto.CreateActivityDTO;
import com.lyy.lottery.service.dto.PageListDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(@Validated @RequestBody CreateActivityParam param){
        log.info("createActivity param:{}", JacksonUtil.writeValueAsString(param));
        CreateActivityDTO activityDTO=activityService.createActivity(param);
        return CommonResult.sucess(convertCreateActivityResult(activityDTO));
    }

    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(PageParam param){
        log.info("findActivityList PageParam：{}",JacksonUtil.writeValueAsString(param));
        PageListDTO<ActivityDTO> pageListDTO=activityService.findActivityList(param);
        return CommonResult.sucess(convertToFindActivityListResult(pageListDTO));
    }

    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(Long activityId){
        log.info("getActivityDetail activityId：{}",activityId);
        ActivityDetailDTO detailDTO=activityService.getActivityDetail(activityId);
        return CommonResult.sucess(convertToGetActivityDetailResult(detailDTO));
    }

    private FindActivityListResult convertToFindActivityListResult(PageListDTO<ActivityDTO> pageListDTO) {
        if (null == pageListDTO){
            throw new ControllerException(ControllerErrorCodeConstants.FIND_ACTIVITY_LIST_ERROR);
        }
        FindActivityListResult result=new FindActivityListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getRecords().stream()
                        .map(activityDTO -> {
                            FindActivityListResult.ActivityInfo activityInfo=new FindActivityListResult.ActivityInfo();
                            activityInfo.setActivityId(activityDTO.getActivityId());
                            activityInfo.setActivityName(activityDTO.getActivityName());
                            activityInfo.setDescription(activityDTO.getDescription());
                            activityInfo.setValid(activityDTO.valid());
                            return activityInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }

    private CreateActivityResult convertCreateActivityResult(CreateActivityDTO activityDTO) {
        if (null == activityDTO){
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult result=new CreateActivityResult();
        result.setActivityId(activityDTO.getActivityId());
        return result;
    }
    private GetActivityDetailResult convertToGetActivityDetailResult(ActivityDetailDTO detailDTO) {
        if (null==detailDTO)
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        GetActivityDetailResult result=new GetActivityDetailResult();

        //活动基本信息
        result.setActivityId(detailDTO.getActivityId());
        result.setActivityName(detailDTO.getActivityName());
        result.setDescription(detailDTO.getDesc());
        result.setValid(detailDTO.valid());

        //关联奖品信息
        result.setPrizes(
                detailDTO.getPrizeDTOList().stream()
                        .sorted(Comparator.comparingInt(prizeDTO -> prizeDTO.getTiers().getCode()))
                        .map(prizeDTO -> {
                            GetActivityDetailResult.Prize prize=new GetActivityDetailResult.Prize();
                            prize.setPrizeId(prizeDTO.getPrizeId());
                            prize.setName(prizeDTO.getName());
                            prize.setImageUrl(prizeDTO.getImageUrl());
                            prize.setPrice(prizeDTO.getPrice());
                            prize.setDescription(prizeDTO.getDescription());
                            prize.setPrizeTierName(prizeDTO.getTiers().getMessage());
                            prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                            prize.setValid(prizeDTO.valid());
                            return prize;
                        }).collect(Collectors.toList())
        );

        //关联用户信息
        result.setUsers(
                detailDTO.getUserDTOList().stream()
                        .map(userDTO -> {
                            GetActivityDetailResult.User user=new GetActivityDetailResult.User();
                            user.setUserId(userDTO.getUserId());
                            user.setUserName(userDTO.getUserName());
                            user.setValid(userDTO.valid());
                            return user;
                        }).collect(Collectors.toList())
        );
        return result;
    }

}